﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.UI;

[assembly: WebResource("com.demo.JScript.js", "text/javascript")]

namespace com.demo
{
// ParseChildren 为真表示，parse 将会把子 Tag 解释为属性
// PersistChildren 为假表示没有子控件
[System.Web.UI.ParseChildren(true)]
[System.Web.UI.PersistChildren(false)]
public class UploadButton : System.Web.UI.WebControls.Button
{
    private UploadProgress progressSetting;

    // 必须设置 NotifyParentProperty 属性
    [NotifyParentProperty(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerDefaultProperty)]
    public UploadProgress UploadProgressSetting
    {
        get
        {
            if (progressSetting == null)
            {
                progressSetting = new UploadProgress();
                if (IsTrackingViewState)
                {
                    ((IStateManager)progressSetting).TrackViewState();
                }
            }

            return this.progressSetting;
        }
        set
        {
            this.progressSetting = value;
        }
    }


    public UploadButton()
        : base()
    {

    }


    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        if (this.UploadProgressSetting.Enabled)
        {
            this.OnClientClick = "uploadProgress();";

            this.Page.ClientScript.RegisterClientScriptResource(
                this.GetType(),
                "com.demo.JScript.js");
        }

    }

    protected override void AddAttributesToRender(HtmlTextWriter writer)
    {
        base.AddAttributesToRender(writer);
        if (this.UploadProgressSetting.Enabled)
        {
            writer.AddAttribute("dueTime", this.progressSetting.DueTime.ToString());
            writer.AddAttribute("period", this.progressSetting.Period.ToString());
            writer.AddAttribute("ajaxEnabled", this.progressSetting.Enabled.ToString().ToLower());
            writer.AddAttribute("callbackFunc", this.progressSetting.CallbackFunc);
            writer.AddAttribute("DiskFileUpload_GUID", this.progressSetting.Guid);

        }
    }

    // 通过基类的 SaveViewState 和  LoadViewState 实现设置的状态管理
    protected override object SaveViewState()
    {
        object first = this.progressSetting.SaveViewState();
        object second = base.SaveViewState();
        return new System.Web.UI.Pair(first, second);
    }

    protected override void LoadViewState(object savedState)
    {
        System.Web.UI.Pair pair = savedState as System.Web.UI.Pair;
        if (pair != null)
        {
            this.progressSetting.LoadViewState(pair.First);
            base.LoadViewState(pair.Second);
        }
    }
}
}
